/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desktopapplication1;

/**
 *
 * @author Lora
 */
public class Ip {
   
    public static final Integer CLASEINVALIDA = 0;
    
    public static final Integer CLASEA = 1;
    public static final Integer CLASEB = 2;
    public static final Integer CLASEC = 3;
    public static final Integer CLASED = 4;
    public static final Integer CLASEE = 5;
    
           
    
    private Integer firstNumber;
    private Integer secondNumber;
    private Integer thirdNumber;
    private Integer fourthNumber;
    private Long numeroCompleto;
    private Boolean valido;
    private Integer clase;

    public Ip() {
        firstNumber = new Integer(192);
        secondNumber = new Integer(168);
        thirdNumber = new Integer(0);
        fourthNumber = new Integer(1);
        multiplicar();
        validar();
    }

    public Ip(Integer firstNumber, Integer secodNumber, Integer thirdNumber, Integer fourthNumber) {
        this();
        this.firstNumber = firstNumber;
        this.secondNumber = secodNumber;
        this.thirdNumber = thirdNumber;
        this.fourthNumber = fourthNumber;
        multiplicar();
        validar();
    }

    public Ip(Long numeroCompleto) {
        this();
        this.numeroCompleto = numeroCompleto;
        dividir();
        validar();
    }

    public Integer getFirstNumber() {
        return firstNumber;
    }

    public void setFirstNumber(Integer firstNumber) {
        this.firstNumber = firstNumber;
        multiplicar();
        validar();
    }

    public Integer getSecondNumber() {
        return secondNumber;
    }

    public void setSecondNumber(Integer secondNumber) {
        this.secondNumber = secondNumber;
        multiplicar();
        validar();
    }

    public Integer getThirdNumber() {
        return thirdNumber;
    }

    public void setThirdNumber(Integer thirdNumber) {
        this.thirdNumber = thirdNumber;
        multiplicar();
        validar();
    }

    public Integer getFourthNumber() {
        return fourthNumber;
    }

    public void setFourthNumber(Integer fourthNumber) {
        this.fourthNumber = fourthNumber;
        multiplicar();
        validar();
    }

    public Long getNumeroCompleto() {
        return numeroCompleto;
    }

    public void setNumeroCompleto(Long numeroCompleto) {
        this.numeroCompleto = numeroCompleto;
        dividir();
        validar();
    }

    public Boolean validar() {
        ipClass();
        if (firstNumber == 0 || firstNumber > 223) {
            valido = false;
            return false;
        }
        valido = true;
        return true;
    }

    public void dividir() {
        Long numeroTemporal = numeroCompleto;
        fourthNumber = new Long(numeroTemporal % 0x100).intValue();
        numeroTemporal = numeroTemporal / 0x100;
        thirdNumber = new Long(numeroTemporal % 0x100).intValue();
        numeroTemporal = numeroTemporal / 0x100;
        secondNumber = new Long(numeroTemporal % 0x100).intValue();
        numeroTemporal = numeroTemporal / 0x100;
        firstNumber = new Long(numeroTemporal % 0x100).intValue();
        numeroTemporal = numeroTemporal / 0x100;
    }

    public void multiplicar() {
        numeroCompleto = new Long(fourthNumber) +
                         new Long(thirdNumber *  0x100) +
                         new Long(secondNumber * 0x10000) +
                         (new Long(firstNumber) * new Long(0x1000000));

    }

    public Boolean getValido() {
        validar();
        return valido;
    }
    
    private void ipClass(){
        if(firstNumber >= 1 && firstNumber <= 126){
            clase = new Integer(CLASEA);
            
        }else if(firstNumber >= 128 && firstNumber <= 191){
            clase = new Integer(CLASEB);
            
        }else if(firstNumber >= 192 && firstNumber <= 223){
            clase = new Integer(CLASEC);
           
        }else if(firstNumber >= 224 && firstNumber <= 239){
            clase = new Integer(CLASED);
            
        }else if(firstNumber >= 240 && firstNumber <= 255){
            clase = new Integer(CLASEE);
            
        }else {
            clase = new Integer(CLASEINVALIDA);
            
        }
        return;
    }

    public Integer getClase() {
        validar();
        return clase;
    }
    
    
}
